package com.adxon.data2;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import com.adxon.MsgException;

public class Link extends Item{
	public final static int KN=13;

	private static void putTags(String tags,Map<String,Integer> map){
		if(tags!=null){
			StringTokenizer z=new StringTokenizer(tags,",");
			while(z.hasMoreTokens()){
				String n=z.nextToken();
				Integer u=map.get(n);
				if(u==null)
					u=0;
				u++;
				map.put(n,u);
			}
		}
	}

	public Link(){
		kn=KN;
	}
	/*@Override
	protected String allocateId()throws MsgException{
		super.allocateId();
		return pk;
	}*/
	@Override
	public void getInfo(HttpServletRequest req)throws MsgException{
		System.out.println("Link: getInfo");
		getAll(req);
		Map<String,Tagging> d=new TreeMap<String,Tagging>();
		Map<String,Integer> g=new TreeMap<String,Integer>();
		Map<Long,Item> w=new TreeMap<Long,Item>();
		Mark o=new Mark();
		o.setRate(19);
		o.setRef(id);
		for(Tagging t:new ItemSet(o)){
			String s=((Item)t).ds;
			if(s!=null)
				d.put(s,t);
			putTags(t.tg,g);
			w.put(t.ow,t.getOwner());
		}
		if(ds!=null)
			d.put(ds,this);
		putTags(tg,g);
		w.remove(ow);
		req.setAttribute("ds",d.values());
		req.setAttribute("tg",g.keySet());
		req.setAttribute("os",w.values());
		req.setAttribute("cn",w.size());
		req.setAttribute("Page","infolink.jsp");
	}
	@Override
	public String getUrl(){
		return pk;
	}
	/*@Override
	public void setPermaLink(String permaLink){
		pk=permaLink;
		try{
			URL u=new URL(pk);
			xt=u.getHost();
		}catch(MalformedURLException e){}
	}
	@Override
	protected void star(){
		try{
			sta=(Kindee)new Mark().update(sto+"/"+id);
		}catch(MsgException e){
			sta=null;
		}
	}*/
}